package com.example.user8899.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import entity.TestDemo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@Slf4j
public class UserController {

    @Value("${server.port}")
    private int port;

    @GetMapping("/user/test")
    public String infos() {
        log.info("商品入口：" + port);
        return "商品入口：" + port;
    }

    @GetMapping("/user/infos_id")
    @HystrixCommand(fallbackMethod = "hystrixTest" )
    public String infosbyid(int id) {
        if (id<0)throw  new RuntimeException("然然哥哥测试Hystrix!");
        return "商品id：" + id + " 端口：" + port;
    }
    public String hystrixTest(int id ){
        return port+" | 不合法数据："+id;
    }

    @PostMapping("/user/postt")
    public ConcurrentHashMap demopost(@RequestBody TestDemo demo) {
        log.info(demo.toString());

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ConcurrentHashMap m = new ConcurrentHashMap();
        m.put("data",demo);
        m.put("port",port);
        return m;
    }


}
